package server;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;

/**
 * User: tum0r
 * Package Name: server
 * Time: 2019-03-11 Monday 19:53:21
 */
public class GameServer extends BaseServer {
    public JSONObject getGameInformation() {
        JSONObject result = JSONObject.fromObject("{}");
        LinkedList<LinkedHashMap<String, Object>> temp = sql.query("select content from setting where name=?", new Object[]{"game"});
        try {
            JSONObject gameInfo = JSONObject.fromObject(temp.get(0).get("content"));

            result.put("name", gameInfo.getString("name"));
            result.put("logo", gameInfo.getString("logo"));
            result.put("sponsor", gameInfo.getString("sponsor"));
            result.put("start", gameInfo.getString("start"));
            result.put("stop", gameInfo.getString("stop"));
            result.put("time_is_up", gameInfo.getString("time_is_up"));
            result.put("punish", gameInfo.getString("punish"));
            result.put("playerCount", sql.query("select * from user where type=? and state=?", new Object[]{0, 1}).size());


            String startTime = gameInfo.getString("start");
            String stopTime = gameInfo.getString("stop");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
            long start = simpleDateFormat.parse(startTime).getTime();
            long stop = simpleDateFormat.parse(stopTime).getTime();

            result.put("startTime", start);
            result.put("stopTime", stop);

            long now = new Date().getTime();
            if (now < start) {
                result.put("state", 0);
            } else if (now <= stop) {
                result.put("state", 1);
            } else {
                result.put("state", 2);
            }
            simpleDateFormat = new SimpleDateFormat("HH:mm");

            String punishTime = gameInfo.getString("punish");
            long punish = simpleDateFormat.parse(punishTime).getTime();

            result.put("punishTime", punish);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    public JSONArray getDynamicRecord(int count) {
        JSONArray result = new JSONArray();
        LinkedList<LinkedHashMap<String, Object>> recordTemp = sql.query("select * from record order by submit_time desc", null);
        LinkedList<LinkedHashMap<String, Object>> subjectTemp;
        LinkedList<LinkedHashMap<String, Object>> userTemp;
        Iterator<LinkedHashMap<String, Object>> recordIterator = recordTemp.iterator();
        LinkedHashMap<String, Object> recordItem;
        LinkedHashMap<String, Object> subjectItem;
        LinkedHashMap<String, Object> userItem;

        int i = 0;

        while (recordIterator.hasNext()) {
            recordItem = recordIterator.next();
            userTemp = sql.query("select username from user where uuid=? and type=? and state=?", new Object[]{recordItem.get("user_id"), 0, 1});
            subjectTemp = sql.query("select title from subject where uuid=?", new Object[]{recordItem.get("subject_id")});
            if (userTemp.size() > 0) {
                userItem = userTemp.get(0);
            } else {
                userItem = new LinkedHashMap<>();
            }
            if (subjectTemp.size() > 0) {
                subjectItem = subjectTemp.get(0);
            } else {
                subjectItem = new LinkedHashMap<>();
            }
            JSONObject item = new JSONObject();
            item.put("user", userItem.get("username") == null ? "" : userItem.get("username"));
            item.put("subject", subjectItem.get("title") == null ? "特殊记录" : subjectItem.get("title"));
            item.put("language", recordItem.get("language") == null ? "" : recordItem.get("language"));
            item.put("result", recordItem.get("result") == null ? "" : recordItem.get("result"));
            item.put("time", recordItem.get("time") == null ? "" : recordItem.get("time"));
            item.put("memory", recordItem.get("memory") == null ? "" : recordItem.get("memory"));
            item.put("submit_time", recordItem.get("submit_time") == null ? "" : recordItem.get("submit_time"));

            if (userItem.containsKey("username")) {
                result.add(item);
                if (++i == count) {
                    break;
                }
            }
        }
        return result;
    }

    public JSONObject getRecordCode(String recordID) {
        JSONObject result = new JSONObject();
        LinkedList<LinkedHashMap<String, Object>> recordTemp = sql.query("select * from record where uuid=?", new Object[]{recordID});
        LinkedList<LinkedHashMap<String, Object>> subjectTemp;
        LinkedList<LinkedHashMap<String, Object>> userTemp;
        Iterator<LinkedHashMap<String, Object>> recordIterator = recordTemp.iterator();
        LinkedHashMap<String, Object> recordItem;
        LinkedHashMap<String, Object> subjectItem;
        LinkedHashMap<String, Object> userItem;

        while (recordIterator.hasNext()) {
            recordItem = recordIterator.next();
            userTemp = sql.query("select username,real_name,sex from user where uuid=?", new Object[]{recordItem.get("user_id")});
            subjectTemp = sql.query("select title, content from subject where uuid=?", new Object[]{recordItem.get("subject_id")});
            if (subjectTemp.size() > 0) {
                subjectItem = subjectTemp.get(0);
            } else {
                subjectItem = new LinkedHashMap<>();
            }
            if (userTemp.size() > 0) {
                userItem = userTemp.get(0);
            } else {
                userItem = new LinkedHashMap<>();
            }
            result.put("user", userItem.get("username") == null ? "" : userItem.get("username"));
            result.put("real_name", userItem.get("real_name") == null ? "" : userItem.get("real_name"));
            result.put("sex", userItem.get("sex") == null ? "" : userItem.get("sex"));
            result.put("title", subjectItem.get("title") == null ? "特殊记录" : subjectItem.get("title"));
            result.put("content", subjectItem.get("content") == null ? "" : subjectItem.get("content"));
            result.put("code", recordItem.get("code") == null ? "" : recordItem.get("code"));
            result.put("language", recordItem.get("language") == null ? "" : recordItem.get("language"));
            result.put("result", recordItem.get("result") == null ? "" : recordItem.get("result"));
            result.put("time", recordItem.get("time") == null ? "" : recordItem.get("time"));
            result.put("memory", recordItem.get("memory") == null ? "" : recordItem.get("memory"));
            result.put("submit_time", recordItem.get("submit_time") == null ? "" : recordItem.get("submit_time"));
        }
        return result;
    }
}
